package com.cooba.adapter;


import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cooba.R;
import com.cooba.bean.NewsLoad;
import com.cooba.utils.ACache;
import com.cooba.utils.Utils;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NewsAdapter extends BaseAdapter {
    private final BitmapUtils bitmapUtils;
    private final ACache acache;
    private final Animation animation;
    private Context context;
    private List<NewsLoad.NewsListEntity> list;
    private final int TYPE_1 = 0;
    private final int TYPE_3 = 1;
    private BitmapDisplayConfig mConfig;
    private Map<Integer, Integer> map;

    public List<NewsLoad.NewsListEntity> getList() {
        return list;
    }

    public void setList(List<NewsLoad.NewsListEntity> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public void addNewData(List<NewsLoad.NewsListEntity> datas) {
        if (datas != null && list != null) {
            this.list.addAll(0, datas);
            this.notifyDataSetChanged();
        }
    }

    public void addMoreData(List<NewsLoad.NewsListEntity> datas) {
        Log.d("wei", "" + datas.size() + "," + list.size());
        if (datas != null && list != null) {
            list.addAll(list.size(), datas);
            this.notifyDataSetChanged();
        }
    }

    public NewsAdapter(Context context, List<NewsLoad.NewsListEntity> list) {
        this.context = context;
        this.list = list;
        map = new HashMap<>();
        animation = AnimationUtils.loadAnimation(context, R.anim.dianzan_animation);
        bitmapUtils = new BitmapUtils(context);
        /**
         * 设置默认的图片展现、加载失败的图片展现
         */
        bitmapUtils.configDefaultLoadingImage(R.mipmap.default_icon1);
        bitmapUtils.configDefaultLoadFailedImage(R.mipmap.default_icon1);
        acache = ACache.get(context);
    }

    @Override
    public int getCount() {
        if (list.size() == 0) {
            return 0;
        }
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        String cover = list.get(position).img_url;
        String[] split = cover.split("@");
        if (split.length != 3) {
            return TYPE_1;
        }
        return TYPE_3;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
        //return super.getViewTypeCount();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        ViewHolder3 holder3 = null;
        int type = getItemViewType(position);
        if (convertView == null) {
            switch (type) {
                case TYPE_1:
                    holder = new ViewHolder();
                    convertView = LayoutInflater.from(context).inflate(R.layout.news_list_item, parent, false);
                    holder.title = (TextView) convertView.findViewById(R.id.tv_tittle);
                    holder.from = (TextView) convertView.findViewById(R.id.news_from);
                    holder.img = (ImageView) convertView.findViewById(R.id.iv);
                    holder.upCount = (TextView) convertView.findViewById(R.id.news_up_count);
                    holder.addOne = (TextView) convertView.findViewById(R.id.animation);
                    convertView.setTag(holder);
                    break;
                case TYPE_3:
                    holder3 = new ViewHolder3();
                    convertView = LayoutInflater.from(context).inflate(R.layout.news_list_item3, parent, false);
                    holder3.title = (TextView) convertView.findViewById(R.id.tv_tittle);
                    holder3.iv_a = (ImageView) convertView.findViewById(R.id.iv_a);
                    holder3.iv_b = (ImageView) convertView.findViewById(R.id.iv_b);
                    holder3.iv_c = (ImageView) convertView.findViewById(R.id.iv_c);
                    holder3.from = (TextView) convertView.findViewById(R.id.news_from);
                    holder3.upCount = (TextView) convertView.findViewById(R.id.news_up_count);
                    holder3.addOne = (TextView) convertView.findViewById(R.id.animation);
                    convertView.setTag(holder3);
                    break;
            }
        } else {
            switch (type) {
                case TYPE_1:
                    holder = (ViewHolder) convertView.getTag();
                    break;
                case TYPE_3:
                    holder3 = (ViewHolder3) convertView.getTag();
                    break;
            }
        }
        String isRead = acache.getAsString("readed_news_list_" + list.get(position).getId());

        switch (type) {
            case TYPE_1:
                if (list.get(position).title != null) {

                    holder.title.setText(Utils.replaceString(list.get(position).getTitle()));//标题
                }
//                if (list.get(position).description != null) {
//                    holder.description.setText(list.get(position).description.replaceAll("\\\\", ""));//描述
//                } else {
//                    holder.description.setText("");
//                }
                if (list.get(position).getAuthor() != null) {
                    holder.from.setText(list.get(position).getAuthor());
                }
                if (!map.containsKey(position)) {
                    int count = (int) (Math.random() * (9999 - 1000 + 1) + 1000);
                    holder.upCount.setText("" + count);
                    map.put(position, count);
                } else {
                    holder.upCount.setText("" + map.get(position));
                }

                if (!TextUtils.isEmpty(isRead) && isRead.equals("true")) {
                    holder.title.setTextColor(context.getResources().getColor(R.color.darkgray));
                } else {
                    holder.title.setTextColor(context.getResources().getColor(R.color.black));
                }
                bitmapUtils.display(holder.img, list.get(position).img_url);
                final ViewHolder finalHolder = holder;
                holder.upCount.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finalHolder.addOne.setVisibility(View.VISIBLE);
                        finalHolder.addOne.startAnimation(animation);
                        new Handler().postDelayed(new Runnable() {
                            public void run() {
                                finalHolder.addOne.setVisibility(View.GONE);
                            }
                        }, 1000);
                        finalHolder.upCount.setText("" + (Integer.parseInt(finalHolder.upCount.getText().toString().trim()) + 1));
                        map.put(position, Integer.parseInt(finalHolder.upCount.getText().toString().trim()));
                    }
                });
                break;
            case TYPE_3:
                if (list.get(position).title != null) {

                    holder3.title.setText(Utils.replaceString(list.get(position).getTitle()));//标题
                }
                if (list.get(position).getAuthor() != null) {
                    holder3.from.setText(list.get(position).getAuthor());
                }
                if (!map.containsKey(position)) {
                    int count = (int) (Math.random() * (9999 - 1000 + 1) + 1000);
                    holder3.upCount.setText("" + count);
                    map.put(position, count);
                } else {
                    holder3.upCount.setText("" + map.get(position));
                }
                if (!TextUtils.isEmpty(isRead) && isRead.equals("true")) {
                    holder3.title.setTextColor(context.getResources().getColor(R.color.darkgray));
                } else {
                    holder3.title.setTextColor(context.getResources().getColor(R.color.black));
                }
                final ViewHolder3 finalHolder3 = holder3;
                holder3.upCount.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finalHolder3.addOne.setVisibility(View.VISIBLE);
                        finalHolder3.addOne.startAnimation(animation);
                        new Handler().postDelayed(new Runnable() {
                            public void run() {
                                finalHolder3.addOne.setVisibility(View.GONE);
                            }
                        }, 1000);
                        finalHolder3.upCount.setText("" + (Integer.parseInt(finalHolder3.upCount.getText().toString().trim()) + 1));
                        map.put(position, Integer.parseInt(finalHolder3.upCount.getText().toString().trim()));
                    }
                });
                String cover = list.get(position).img_url;
                String[] slit = cover.split("@");
                bitmapUtils.display(holder3.iv_a, slit[0]);
                bitmapUtils.display(holder3.iv_b, slit[1]);
                bitmapUtils.display(holder3.iv_c, slit[2]);
                break;
        }

        return convertView;
    }


    class ViewHolder {
        public TextView title;
        public TextView description;
        public ImageView img;
        public TextView from;
        public TextView upCount;
        public TextView addOne;
    }

    class ViewHolder3 {
        public TextView title;
        public ImageView iv_a, iv_b, iv_c;
        public TextView from;
        public TextView upCount;
        public TextView addOne;
    }
}
